<template>
  <div class="checkout-container">
    <!-- 品牌装饰背景 -->
    <div class="brand-decoration"></div>

    <!-- 页面头部 -->
    <div class="header">
      <div class="brand-logo">
        <span>COKFOOD</span>
      </div>
      <h1>Help {{ orderData?.list[0]?.uname }} Order</h1>
      <p>You're sending a warm gesture to your friend</p>
    </div>

    <!-- 限时优惠模块 -->
  <!--  <div class="promo-section fade-in" v-if="orderData.countdown">
      <div class="promo-badge">Limited Offer</div>
      <div class="promo-header">
        <div class="promo-title">Limited Time Offer</div>
      </div>
      <CountdownTimer :totalSeconds="orderData.countdown" />
      <div class="promo-message">Complete your payment before this offer expires</div>
    </div> -->

    <!-- 订单详情模块 -->
    <div class="order-section">
      <h2 class="section-title">Your Order</h2>

      <div class="order-item fade-in" v-for="(item, index) in orderData.list" :key="item.id" :style="{ animationDelay: `${index * 0.1}s` }">
        <img
          :src="item.goodsDataOne && item.goodsDataOne.head_img && item.goodsDataOne.head_img.length ? item.goodsDataOne.head_img[0] : ''"
          :alt="item.goods_name"
          class="item-image"
        >
        <div class="item-details">
          <div>
            <div class="item-name">{{ item.goods_name }}</div>
            <div class="item-spec">
              <span class="spec-tag" v-if="item.remark">{{ item.remark }}</span>
            </div>
          </div>
          <div class="item-bottom">
            <span class="item-quantity">x{{ item.order_number }}</span>
            <div class="item-price">${{ (item.order_money * item.order_number).toFixed(2) }}</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 配送信息卡片 -->
    <div class="delivery-card fade-in" style="animation-delay: 0.3s;">
      <!-- <div class="delivery-time">
        <div class="time-icon">⏱</div>
        <div class="time-info">
          <div class="time-label">Estimated delivery</div>
          <div class="time-value">35-45 minutes</div>
        </div>
      </div> -->
      <div class="address-info">
        <div class="address-line" v-if=" orderData.address">
          <span class="address-icon">📍</span>
          <span>{{ orderData.address }}</span>
        </div>
        <div class="address-line">
          <span class="address-icon">👤</span>
          <span>{{ orderData?.list[0]?.uname }}</span>
        </div>
      </div>
    </div>

    <!-- 费用明细模块 -->
    <div class="price-summary fade-in" style="animation-delay: 0.4s;">
      <div class="price-row">
        <span class="price-label">Original Price</span>
        <span class="price-value">${{ orderData.original_price && orderData.original_price.toFixed(2) }}</span>
      </div>
	  <div class="price-row" v-if="orderData.shipping_fee > 0">
	    <span class="price-label">Shipping Fee</span>
	    <span class="price-value">${{ orderData.shipping_fee.toFixed(2)  }}</span>
	  </div>
      <div class="price-row" v-if="orderData.coupon_money > 0">
        <span class="price-label">Coupon Money</span>
        <span class="price-value price-discount">-${{ orderData.coupon_money && orderData.coupon_money.toFixed(2) }}</span>
      </div>
	 
      <div class="price-row">
        <span class="price-label">Total Items</span>
        <span class="price-value">{{ orderData.total_num }}</span>
      </div>
      <div class="price-total">
        <div class="price-row">
          <span class="price-label">Total</span>
          <span class="price-value">${{ orderData.total_money && orderData.total_money.toFixed(2) }}</span>
        </div>
      </div>
    </div>

    <!-- 支付操作区 -->
    <div class="payment-section">
      <div class="payment-tips">{{ orderData?.list[0]?.uname }} will receive a notification when you place this order</div>
      <button class="paypal-button" @click="goToPay(orderData.payStatus)" :disabled="isProcessing">
        <span v-if="!isProcessing">{{ PayName }}</span>
        <span v-else><span class="loading"></span> Processing...</span>
      </button>
      <div class="security-badges">
        <div class="security-item">
          <div class="security-icon">🔒</div>
          <span>Secure<br>Payment</span>
        </div>
        <div class="security-item">
          <div class="security-icon">⚡</div>
          <span>Fast<br>Processing</span>
        </div>
        <div class="security-item">
          <div class="security-icon">📍</div>
          <span>Live<br>Tracking</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang='ts' setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getUserOrderDataApi, payPalIndexApi } from '@/api/index'
import { useRoute, useRouter } from 'vue-router';
import CountdownTimer from '@/components/CountdownTimer.vue'
const router = useRouter()
// 定义订单项接口
interface OrderItem {
  id: number
  goods_name: string
  order_money: number
  order_number: number
  remark?: string
  uname?: string
  goodsDataOne?: {
    head_img?: string[]
  }
}

// 定义订单数据接口
interface OrderData {
  countdown: number
  list: OrderItem[]
  original_price: number
  coupon_money: number
  total_num: number
  total_money: number
  address: string
}

const route = useRoute()
const orderNo = ref('')
const isProcessing = ref(false)
let PayName = 'Pay with PayPal'
const queryOrderNo = route.query.orderNo
orderNo.value = typeof queryOrderNo === 'string' ? queryOrderNo : ''

const goToPay = async (payStatus) => {

  if (isProcessing.value) return

  isProcessing.value = true
  try {
    const res = await payPalIndexApi({ order_no: orderNo.value})

    console.log(8978978989, res);

    if(res.code != 200) {
      isProcessing.value = false
      return ElMessage({
        type: 'error',
        message: res.msg
      })
    }

    window.location.href = res.data.pay_url
  } catch (error: any) {
    isProcessing.value = false
    ElMessage({
      type: 'error',
      message: error.msg
    })
  }
}

const orderData = ref<OrderData>({
  countdown: 0,
  list: [],
  original_price: 0,
  coupon_money: 0,
  total_num: 0,
  total_money: 0,
  address: ''
})

const getUserOrderDataRequest = async () => {
  try {
    const res = await getUserOrderDataApi({
      orderNo: orderNo.value,
    })
    Object.assign(orderData.value, res.data)
    if (res.data.payStatus == 0){
      return;
    }
    if (res.data.payStatus == 1){
      //console.log(res.data.payStatus)
        PayName = 'Expired'
      router.push('/expired')
    }
      if (res.data.payStatus == 2){
       // console.log(res.data.payStatus)
           PayName = 'Already paid'
       router.push('/paysuccess')
      }


  } catch (error: any) {
    ElMessage({
      type: 'error',
      message: error.msg
    })
  }
}

onMounted(() => {
  getUserOrderDataRequest()

  // 添加页面加载动画
  const fadeElements = document.querySelectorAll('.fade-in')
  fadeElements.forEach((element, index) => {
    (element as HTMLElement).style.animationDelay = `${index * 0.1}s`
  })
})
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.checkout-container {
  max-width: 430px;
  margin: 0 auto;
  background-color: #FFFFFF;
  min-height: 100vh;
  position: relative;
  box-shadow: 0 0 40px rgba(0, 0, 0, 0.08);
}

/* 品牌装饰背景 */
.brand-decoration {
  position: absolute;
  width: 100%;
  height: 300px;
  top: 0;
  left: 0;
  background: linear-gradient(135deg, #EF4444 0%, #DC2626 100%);
  overflow: hidden;
  z-index: 0;
}

.brand-decoration::before {
  content: '';
  position: absolute;
  width: 150%;
  height: 150%;
  top: -50%;
  right: -50%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 0%, transparent 50%);
  animation: float 20s ease-in-out infinite;
}

.brand-decoration::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 0;
  width: 100%;
  height: 80px;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1440 320'%3E%3Cpath fill='%23FFFFFF' fill-opacity='1' d='M0,96L48,112C96,128,192,160,288,160C384,160,480,128,576,112C672,96,768,96,864,112C960,128,1056,160,1152,160C1248,160,1344,128,1392,112L1440,96L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z'%3E%3C/path%3E%3C/svg%3E") no-repeat bottom;
  background-size: cover;
}

@keyframes float {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  50% { transform: translateY(-20px) rotate(180deg); }
}

/* 页面头部 */
.header {
  position: relative;
  padding: 40px 20px 100px;
  text-align: center;
  color: white;
  z-index: 1;
}

.brand-logo {
  font-size: 32px;
  font-weight: 800;
  letter-spacing: -1px;
  margin-bottom: 20px;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.brand-logo span {
  background: linear-gradient(45deg, #FFFFFF 30%, #FEE2E2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header h1 {
  font-size: 26px;
  font-weight: 600;
  margin-bottom: 10px;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.header p {
  font-size: 15px;
  opacity: 0.95;
  font-weight: 300;
}

/* 限时优惠模块 */
.promo-section {
  margin: -60px 20px 20px;
  padding: 25px;
  background: linear-gradient(135deg, #FFF5F5 0%, #FEE2E2 100%);
  border-radius: 20px;
  box-shadow: 0 8px 30px rgba(239, 68, 68, 0.15);
  position: relative;
  overflow: hidden;
  z-index: 2;
  border: 1px solid rgba(239, 68, 68, 0.1);
}

.promo-section::before {
  content: '';
  position: absolute;
  top: -100%;
  right: -100%;
  width: 300%;
  height: 300%;
  background: conic-gradient(from 0deg at 50% 50%, transparent 0deg, rgba(239, 68, 68, 0.05) 60deg, transparent 120deg);
  animation: rotate 30s linear infinite;
}

@keyframes rotate {
  100% { transform: rotate(360deg); }
}

.promo-badge {
  position: absolute;
  top: -10px;
  right: 20px;
  background: linear-gradient(135deg, #EF4444, #DC2626);
  color: white;
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
  box-shadow: 0 4px 12px rgba(239, 68, 68, 0.3);
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}

.promo-header {
  text-align: center;
  margin-bottom: 20px;
  position: relative;
}

.promo-title {
  color: #DC2626;
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.promo-title::before,
.promo-title::after {
  content: '✨';
  font-size: 16px;
  animation: sparkle 1.5s ease-in-out infinite;
}

@keyframes sparkle {
  0%, 100% { opacity: 0.5; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.2); }
}

.promo-message {
  text-align: center;
  color: #DC2626;
  font-size: 14px;
  font-weight: 500;
  padding: 10px 20px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 30px;
  display: inline-block;
  margin: 15px auto 0;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
}

/* 订单详情模块 */
.order-section {
  margin: 30px 20px;
}

.section-title {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 20px;
  color: #1F2937;
  display: flex;
  align-items: center;
  gap: 10px;
  position: relative;
}

.section-title::before {
  content: '';
  width: 4px;
  height: 24px;
  background: linear-gradient(135deg, #EF4444, #DC2626);
  border-radius: 2px;
  box-shadow: 0 2px 8px rgba(239, 68, 68, 0.3);
}

.section-title::after {
  content: '';
  flex: 1;
  height: 1px;
  background: linear-gradient(90deg, #E5E7EB, transparent);
  margin-left: 10px;
}

.order-item {
  display: flex;
  gap: 16px;
  padding: 18px;
  background: #FFFFFF;
  border-radius: 16px;
  margin-bottom: 16px;
  transition: all 0.3s ease;
  border: 1px solid #F3F4F6;
  position: relative;
  overflow: hidden;
}

.order-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, transparent, rgba(239, 68, 68, 0.02));
  opacity: 0;
  transition: opacity 0.3s ease;
}

.order-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.08);
  border-color: #FEE2E2;
}

.order-item:hover::before {
  opacity: 1;
}

.item-image {
  width: 90px;
  height: 90px;
  border-radius: 16px;
  object-fit: cover;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.item-details {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.item-name {
  font-size: 17px;
  font-weight: 600;
  color: #1F2937;
  margin-bottom: 6px;
  line-height: 1.3;
}

.item-spec {
  font-size: 13px;
  color: #6B7280;
  margin-bottom: 10px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.spec-tag {
  background: #F3F4F6;
  padding: 3px 8px;
  border-radius: 6px;
  font-size: 12px;
}

.item-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.item-quantity {
  font-size: 14px;
  color: #6B7280;
}

.item-price {
  font-size: 20px;
  font-weight: 700;
  color: #EF4444;
}

/* 配送信息卡片 */
.delivery-card {
  margin: 30px 20px;
  padding: 25px;
  background: linear-gradient(135deg, #FAFAFA 0%, #F3F4F6 100%);
  border-radius: 20px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
  border: 1px solid #E5E7EB;
  position: relative;
  overflow: hidden;
}

.delivery-card::before {
  content: '🚀';
  position: absolute;
  right: 20px;
  top: 20px;
  font-size: 60px;
  opacity: 0.1;
  transform: rotate(-15deg);
}

.delivery-time {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 20px;
}

.time-icon {
  width: 50px;
  height: 50px;
  background: linear-gradient(135deg, #EF4444, #DC2626);
  border-radius: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
  box-shadow: 0 4px 15px rgba(239, 68, 68, 0.25);
  animation: bounce 2s ease-in-out infinite;
}

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-5px); }
}

.time-info {
  flex: 1;
}

.time-label {
  font-size: 14px;
  color: #6B7280;
  font-weight: 500;
}

.time-value {
  font-size: 20px;
  font-weight: 700;
  color: #1F2937;
  display: flex;
  align-items: center;
  gap: 8px;
}

.time-value::after {
  content: '⚡';
  font-size: 16px;
  animation: flash 1.5s ease-in-out infinite;
}

@keyframes flash {
  0%, 100% { opacity: 0.3; }
  50% { opacity: 1; }
}

.address-info {
  padding-top: 20px;
  border-top: 2px dashed #E5E7EB;
}

.address-line {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
  font-size: 15px;
  color: #374151;
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 10px;
  transition: all 0.3s ease;
}

.address-line:hover {
  background: rgba(255, 255, 255, 0.8);
  transform: translateX(5px);
}

.address-icon {
  font-size: 20px;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(239, 68, 68, 0.1);
  border-radius: 8px;
}

/* 费用明细模块 */
.price-summary {
  margin: 30px 20px;
  padding: 25px;
  background: linear-gradient(135deg, #FFFBFB 0%, #FEF5F5 100%);
  border-radius: 20px;
  border: 2px dashed rgba(239, 68, 68, 0.2);
  position: relative;
}

.price-summary::before {
  content: '💰';
  position: absolute;
  right: 20px;
  bottom: 20px;
  font-size: 50px;
  opacity: 0.1;
}

.price-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  font-size: 16px;
  padding: 8px 0;
}

.price-label {
  color: #6B7280;
  font-weight: 500;
}

.price-value {
  color: #1F2937;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.price-discount {
  color: #10B981;
  font-weight: 700;
}

.price-total {
  padding-top: 20px;
  border-top: 2px solid rgba(239, 68, 68, 0.2);
  margin-top: 20px;
  position: relative;
}

.price-total::before {
  content: 'Total';
  position: absolute;
  top: -12px;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(135deg, #FFFBFB 0%, #FEF5F5 100%);
  padding: 0 15px;
  color: #EF4444;
  font-size: 14px;
  font-weight: 600;
}

.price-total .price-row {
  margin-bottom: 0;
}

.price-total .price-label {
  font-size: 20px;
  font-weight: 700;
  color: #1F2937;
}

.price-total .price-value {
  font-size: 28px;
  font-weight: 800;
  background: linear-gradient(135deg, #EF4444, #DC2626);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* 支付操作区 */
.payment-section {
  position: sticky;
  bottom: 0;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.95) 0%, rgba(255, 255, 255, 1) 100%);
  padding: 25px 20px;
  box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.08);
  border-top: 1px solid #E5E7EB;
  backdrop-filter: blur(10px);
}

.payment-tips {
  text-align: center;
  font-size: 13px;
  color: #6B7280;
  margin-bottom: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

.payment-tips::before {
  content: '💡';
}

.paypal-button {
  width: 100%;
  background: linear-gradient(135deg, #0070BA 0%, #005EA6 100%);
  color: white;
  padding: 18px;
  border: none;
  border-radius: 16px;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  box-shadow: 0 8px 25px rgba(0, 112, 186, 0.3);
  position: relative;
  overflow: hidden;
}

.paypal-button::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 0;
  height: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  transform: translate(-50%, -50%);
  transition: width 0.6s, height 0.6s;
}

.paypal-button:hover::before {
  width: 400px;
  height: 400px;
}

.paypal-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 35px rgba(0, 112, 186, 0.4);
}

.paypal-button:active {
  transform: translateY(0);
}

.paypal-button:disabled {
  opacity: 0.8;
  cursor: not-allowed;
}

.security-badges {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  padding: 15px;
  background: #F9FAFB;
  border-radius: 12px;
  gap: 15px;
}

.security-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #6B7280;
  text-align: center;
}

.security-icon {
  width: 32px;
  height: 32px;
  background: linear-gradient(135deg, #10B981 0%, #059669 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 16px;
  box-shadow: 0 3px 10px rgba(16, 185, 129, 0.3);
}

/* Loading 动画 */
.loading {
  display: inline-block;
  width: 22px;
  height: 22px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top: 3px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 动画效果 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.fade-in {
  animation: fadeInUp 0.6s ease-out;
  animation-fill-mode: both;
}

/* 响应式设计 */
@media (max-width: 430px) {
  .checkout-container {
    box-shadow: none;
  }
}
</style>
